﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class Attendence : System.Web.UI.Page
    {
        private readonly Student blstd = new Student();
        private readonly BLRoutine _blroutine = new BLRoutine();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DDLSection.Enabled = false;
                DDLSubject.Enabled = false;
                GridViewStudentList.Visible = false;
                LoadClass();

            }

        }
        public void LoadClass()
        {
            List<Grade> grade = _blroutine.GetGrade();
            DDLClass.Items.Clear();
            DDLClass.Items.Insert(0, new ListItem("Class", "0"));
            DDLClass.DataSource = grade;
            DDLClass.DataTextField = "ClassName";
            DDLClass.DataValueField = "ClassId";
            DDLClass.DataBind();
        
        }
        public void LoadSection(int grade)
        {
            List<MStudent> section = _blroutine.GetSection(grade);
            DDLSection.Items.Clear();
            DDLSection.Items.Insert(0, new ListItem("Section", "0"));
            DDLSection.DataSource = section;
            DDLSection.DataTextField = "Section";
            DDLSection.DataValueField = "Section";
            DDLSection.DataBind();

        }

        public void LoadStudent(int classid, string section)
        {
            
            List<MStudent> lstd = new List<MStudent>();
            lstd = blstd.GetStudentListByClassSection(classid, section);
            GridViewStudentList.DataSource = lstd;
            GridViewStudentList.DataBind();

        }
        public void LoadSubject(int classid)
        {
            List<Subject> lsub = new List<Subject>();
            lsub = _blroutine.GetSubjectByClassId(classid);
            DDLSubject.Items.Clear();
            DDLSubject.Items.Insert(0, new ListItem("--Subject--", "0"));
            DDLSubject.DataSource = lsub;
            DDLSubject.DataTextField = "SubjectName";
            DDLSubject.DataValueField = "SubjectId";
            DDLSubject.DataBind();
       }

        protected void DDLClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLSection.Enabled = true;
            DDLSubject.Enabled = false;
            GridViewStudentList.Visible = false;
            int grade = Int32.Parse(DDLClass.SelectedItem.Value);
            LoadSection(grade);
        }
        protected void DDLSection_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLSubject.Enabled = true;
            GridViewStudentList.Visible = false;
            int classid = Int32.Parse(DDLClass.SelectedItem.Value);
            LoadSubject(classid);
           
        }
        protected void DDLSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewStudentList.Visible = true;
            int classid = Int32.Parse(DDLClass.SelectedItem.Value);
            string section = DDLSection.SelectedItem.Value;
            LoadStudent(classid, section);

        }
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            
            foreach (GridViewRow row in GridViewStudentList.Rows)
            {
                Attend attendence = new Attend();
                
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox Present = (CheckBox)row.Cells[0].FindControl("ChkPresent");
                    CheckBox Absent = (CheckBox)row.Cells[0].FindControl("ChkAbsent");
                    CheckBox Late = (CheckBox)row.Cells[0].FindControl("ChkLate");
                    attendence.Present = Present.Checked;
                    attendence.Absent = Absent.Checked;
                    attendence.Late = Late.Checked;
                                        
                    attendence.StudentId= Int32.Parse(row.Cells[0].Text);
                    attendence.SubjectId = Int32.Parse(DDLSubject.SelectedItem.Value);
                    int result = blstd.SaveAttendence(attendence);
                    if (result > 0)
                    {
                        Response.Write("Attended");
                    }
                           // DataTable dt = DataManager.DeleteNotification(notifyid);
                            //loadgrid();

                }
            }
        }

       
        
    }
}